/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.vale.controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;

/**
 *
 * @author Gabriel Bran gbran.u@gmail.com
 */
public abstract class AbstractController {

    protected static final Logger LOGGER = Logger.getLogger(AbstractController.class);

    protected FacesContext getFacesContext() {
        return FacesContext.getCurrentInstance();
    }

    protected HttpServletRequest getHttpServletRequest() {
        return (HttpServletRequest) getFacesContext().getExternalContext().getRequest();
    }

    protected void addInformationMessage(String message) {
        addMessage("Information", message, FacesMessage.SEVERITY_INFO);
    }

    protected void addWarningMessage(String message) {
        addMessage("Warning", message, FacesMessage.SEVERITY_WARN);
    }

    protected void addErrorMessage(String message) {
        addMessage("Error", message, FacesMessage.SEVERITY_ERROR);
    }

    private void addMessage(String title, String message, FacesMessage.Severity severity) {
        getFacesContext().addMessage(null, new FacesMessage(severity, title, message));
    }

    protected void _login(String username, String password) throws ServletException {
        HttpServletRequest request = getHttpServletRequest();
        request.login(username, password);
    }

    protected void _logout() {
        getFacesContext().getExternalContext().invalidateSession();
    }

    public String getParam(String paramName) {
        Map<String, String> params = getFacesContext().getExternalContext().getRequestParameterMap();
        return params.get(paramName);
    }

    public void putOnFlash(String name, Object object) {
        getFacesContext().getExternalContext().getFlash().put(name, object);
    }

    public <T extends Object> T getFromFlash(String name) {
        return (T) getFacesContext().getExternalContext().getFlash().get(name);
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(String beanName) {
        FacesContext context = FacesContext.getCurrentInstance();
        return (T) context.getApplication().evaluateExpressionGet(context, "#{" + beanName + "}", Object.class);
    }

    public List<SelectItem> getValueList() {
        HashMap<String, String> m = new HashMap<>();
        List<SelectItem> l = new ArrayList<>();
        for (Entry<String, String> e : m.entrySet()) {
            l.add(new SelectItem(e.getValue(), e.getKey()));
        }
        return l;
    }

    public String goHome() {
        return "home";
    }
}
